<?php
class adService extends commonService{
    
    public function __construct(){
        parent::__construct();
    }
    public function t(){
        return array('ad');
    }

    //文件写入
	private function _put($file,$data){
		$dir=dirname($file);
		if(!is_dir($dir)) mkdir($dir,0777,true);
		return file_put_contents($file,$data);
	}
    //批量更新arc html
	public function make_html($items){
		if(!is_array($items)) return false;
		if(!count($items)) return false;
		foreach($items as $k=>$v) $this->_html($v);
		return true;
	}
    //生成
	private function _html($id){
		$query="select * from `{$this->t[0]}` where `id`=:id limit 1";
		$item=$this->m->select($query,array('id'=>$id));
		if(!is_array($item)) return false;
        if(!$item['status']) return false;
        $file=ROOT.'ad/'.$id.'.html';
		$this->_put($file,$item['code']);
		return true;
	}
    //编辑 删除时用
	private function unlink_html($items){
		if(!is_array($items)) return false;
		if(!count($items)) return false;
		foreach($items as $k=>$v){
			$query="select `id` from `{$this->t[0]}` where `id`=:id limit 1";
			$r=$this->m->select($query,array('id'=>$v));
			if(!$r['id']) continue;
			$file=ROOT.'ad/'.$r['id'].'.html';
			if(file_exists($file)) unlink($file);
		}
		return true;
	}
    //清空文件
    private function empty_html($items){
        if(!is_array($items)) return false;
		if(!count($items)) return false;
		foreach($items as $k=>$v){
			$query="select `id` from `{$this->t[0]}` where `id`=:id limit 1";
			$r=$this->m->select($query,array('id'=>$v));
			if(!$r['id']) continue;
			$file=ROOT.'ad/'.$r['id'].'.html';
			$this->_put($file,'');
		}
		return true;
    }
    //批量更新arc status
	public function update_status($items,$d){
		$str=implode(',',$items);
		$this->m->query("update `{$this->t[0]}` set `status`=:d where `id` in ($str)",array('d'=>$d));
		if($d==1){
			$this->make_html($items);
		}else{
			$this->empty_html($items);
		}
		return true;
	}

    public function add_($post){
		
        $result=array();
        
        $vali=validate::check('ad',$post,'add');
        if($vali){
            $result[0]=$this->model->_i($this->t[0],$post);
            if($result[0]){
                $insert_id=$this->m->insert_id();
                if($post['status']){
                    $this->make_html(array($insert_id));
                }
            }
        }else{
            $result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
        }
        return $result;
    }
    public function mod($id){
        $data=$this->model->_f($this->t[0],'*',$id);
		return $data;
    }
	
    public function mod_($post,$id){
        $result=array();
        
        $vali=validate::check('ad',$post,'mod',array('id::!='=>$id));
        if($vali){
    		$result[0]=$this->model->_u($this->t[0],$post,$id);
            if($result[0]){
                if($post['status']){
                    $this->make_html(array($id));
                }else{
                    $this->empty_html(array($id));
                }
            }
    	}else{
    		$result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
    	}
        return $result;
    }
    public function del($id){
        $return=array();
        $this->unlink_html(array($id));//删html
		$return[0]=$this->model->_d($this->t[0],$id);
        return $return;
    }
    public function li($condition=array(),$extra='',$extra_data=array()){
        $return=array();
        $query="select * from `{$this->t[0]}` where 1 {$extra} order by `id` desc";
        $p=new smart_pager(array($query,$extra_data),$this->m);
        //$p->address('&block_id=%block_id%','block_id',$condition['block_id']);
		foreach($condition as $k=>$v){
			$p->address("&{$k}=%{$k}%",$k,$v);
		}
        if($p->total_num>0){
            $return[]=$this->m->mselect($p->query,$extra_data);
            $return[]=$p;
        }
        return $return;
    }
    public function return_min_max(){
		$query="select min(`id`),max(`id`) from `{$this->t[0]}` where `status`='1'";
		return $this->m->select($query,array(),'NUM');
	}
    private function _gpc($name){
        //$a=array_merge($_GET,$_POST);
        //return $a[$name];
        if($_POST) return $_POST[$name];
        if($_GET) return $_GET[$name];
    }
    //ok
	public function html_show($data=array()){
		$m='ad';
		$a='html';
		$aa='html_';
		$k='show';
		

        $fid=$this->_gpc('fid');//first id
        $sid=$this->_gpc('sid');//start id
        $tid=$this->_gpc('tid');//total id
        $num=$this->_gpc('num');
        
        
		$sql = '';
		$d = array() ;
		
		if(!isset($fid)){
			$query="select min(`id`) from `{$this->t[0]}` where `status`='1' {$sql}";
			$fid=intval($this->m->get_field($query,$d));
		}
		isset($sid) or $sid=$fid;
		if(!isset($tid)){
			$query="select max(`id`) from {$this->t[0]} where `status`='1' {$sql}";
			$tid=intval($this->m->get_field($query,$d));
		}
		isset($num) or $num=100;
		if($fid<=$tid){
			$query="select `id` from {$this->t[0]} where `status`='1' and `id`>=:id {$sql} order by `id` asc limit 0,$num";
			$info=$this->m->mselect($query,array_merge($d,array('id'=>$fid)));
			if(isset($info[0]['id']) and is_array($info)){
				foreach($info as $_key=>$_value){
					$itemid=$_value['id'];
					$this->_html($itemid);////
				}
				$itemid+=1;
			}else{
				$itemid=$fid+$num;
			}
		}else{
			jump($m,$a);//生成完毕跳转
		}
		$msg='ID从'.$fid.'至'.($itemid-1).'生成成功'.$this->progress($sid, $fid, $tid);
		$url=url($m,$aa,"k=$k&sid=$sid&fid=$itemid&tid=$tid&num=$num");
		include(VIEWER_DIR.'shell/tip.php');
		exit;
	}
    //生成静态部分
	private function progress($sid, $fid, $tid) {
		if($tid > $sid && $fid < $tid) {
			$p = round(($fid-$sid)*100/($tid-$sid));
			if($p > 100) $p = 100;
			$p = $p.'%';
		} else {
			$p = '100%';
		}
		return '<table cellpadding="0" cellspacing="0" width="100%" style="margin:0"><tr><td><div class="progress"><div style="width:'.$p.';">&nbsp;</div></div></td><td style="color:#666666;font-size:10px;width:40px;text-align:center;">'.$p.'</td></tr></table>';
	}
//
}